﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Duckbill
{
    public class StateManager
    {
        public List<BaseState> states = new List<BaseState>();

        public void Push(BaseState state)
        {
            states.Add(state);
        }
        public void Pop()
        {
            states.RemoveAt(states.Count-1);
        }
        public BaseState Top()
        {
            return states[states.Count-1];
        }

        public void Update(float time)
        {
            states[states.Count-1].Update(time);
        }
        public void Draw(SpriteBatch batch)
        {
            states[states.Count-1].Draw(batch);
        }
    }

    public enum TransitionType
    {
        fade,
        rightToLeft,
        leftToRight,
        topToBottom,
        bottomToTop
    }

    public class TransitionState : BaseState
    {
        BaseState state1, state2;
        TransitionType transition;
        float transitionTime;
        float currTime=0f;

        public TransitionState(BaseState state1, BaseState state2,
            TransitionType transition=TransitionType.fade, float transitionTime=0.75f)
        {
            this.state1 = state1;
            this.state2 = state2;
            this.transition = transition;
            this.transitionTime = transitionTime;
        }
        public override void Update(float time)
        {
            currTime += time;
            if (currTime > transitionTime)
            {
                Game.states.Pop(); //remove this state
                Game.states.Push(state2); //switch to other state
            }
        }
        public override void Draw(SpriteBatch batch)
        {
            float t = currTime/transitionTime;
            float dX = t*Game.SCREEN_WIDTH;
            float dY = t*Game.SCREEN_HEIGHT;

            switch (transition)
            {
                case TransitionType.fade:
                    Sprite.drawColor = new Color(1f-t, 1f-t, 1f-t, 1f-t);
                    state1.Draw(batch);

                    Sprite.drawColor = new Color(1, 1, 1, t);
                    state2.Draw(batch);
                    break;
                case TransitionType.rightToLeft:
                    Game.player.visible = false;
                    Sprite.offset = new Vector2(dX, 0);
                    state1.Draw(batch);

                    Game.player.visible = true;
                    Sprite.offset = new Vector2(dX - Game.SCREEN_WIDTH, 0);
                    state2.Draw(batch);

                    Sprite.offset = Vector2.Zero;
                    break;
                case TransitionType.leftToRight:
                    Game.player.visible = false;
                    Sprite.offset = new Vector2(-dX, 0);
                    state1.Draw(batch);

                    Game.player.visible = true;
                    Sprite.offset = new Vector2(Game.SCREEN_WIDTH - dX, 0);
                    state2.Draw(batch);

                    Sprite.offset = Vector2.Zero;
                    break;
                case TransitionType.bottomToTop:
                    Game.player.visible = false;
                    Sprite.offset = new Vector2(0, dY);
                    state1.Draw(batch);

                    Game.player.visible = true;
                    Sprite.offset = new Vector2(0, dY - Game.SCREEN_HEIGHT);
                    state2.Draw(batch);

                    Sprite.offset = Vector2.Zero;
                    break;
                case TransitionType.topToBottom:
                    Game.player.visible = false;
                    Sprite.offset = new Vector2(0, -dY);
                    state1.Draw(batch);

                    Game.player.visible = true;
                    Sprite.offset = new Vector2(0, Game.SCREEN_HEIGHT - dY);
                    state2.Draw(batch);

                    Sprite.offset = Vector2.Zero;
                    break;
            }
        }
    }
}
